package entity;

/**
 * 商品类
 * 包含商品id，名称，价格，图片，产地
 */
public class Commodity {

    private int id;
    private String name;
    private double price;
    //库存
    private int number;
    private String img;
    private String address;


    public Commodity() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Commodity(int id, String name, double price, int number, String img, String address) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.number = number;
        this.img = img;
        this.address = address;
    }

    public void getCommodity() {
        System.out.print("商品" + this.getId() + "：名称-" + this.getName() + " ,价格-" + this.getPrice() + " ,产地-" + this.getAddress());
    }

    //保证不添加重复商品，重写两个方法

    @Override
    public int hashCode() {
        //相同的字符串，会生成相同的hashcode
        return this.getId() + this.getName().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Commodity) {
            Commodity c = (Commodity) obj;
            if (this.getId() == c.getId() && this.getName().equals(c.getName())) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
